﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;


namespace kelvinDigital.USiteBuilderAdmin
{
    public partial class umbraco_plugins_uSiteBuilderAdmin_CompareResults : System.Web.UI.UserControl
    {
        public List<ContentComparison> DataSource { get; set; }
        public string ParentAlias { get; set; }
        public event EventHandler ItemDeleted;

        public void OnItemDeleted(EventArgs e)
        {
            EventHandler handler = ItemDeleted;
            if (handler != null) handler(this, e);
        }


        protected void rptDeleted_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                new UmbracoManager().DeleteDocumentType(e.CommandArgument.ToString());
                OnItemDeleted(new EventArgs());
            }
        }

        protected void rptDeleted_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                rptDeleted.Visible = true;
            }
        }

        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
            var umbracoManager = new UmbracoManager();
            foreach (var docType in DataSource.Where(d => d.DocumentTypeStatus == Status.Deleted))
            {
                umbracoManager.DeleteDocumentType(docType.Alias);
            }

            OnItemDeleted(new EventArgs());
        }



        public void BindGrid()
        {
            rptList.Data = DataSource;
            rptList.ParentAlias = ParentAlias;
            rptList.DataBind();

            IEnumerable<ContentComparison> deleted = DataSource.Where(d => d.DocumentTypeStatus == Status.Deleted);
            rptDeleted.DataSource = deleted;
            rptDeleted.DataBind();
            rptDeleted.Visible = deleted.Any();
        }




    }
}